package controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.appengine.api.datastore.Text;

@SuppressWarnings("serial")
public class LookupIpServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		resp.setHeader( "Cache-Control", "public, max-age=86400");
		resp.setHeader( "Expires", new Date(Calendar.getInstance().getTime().getTime()+24*60*60*1000).toGMTString());
		String ip = req.getParameter("ip");
		
		String jsp = "-";
		String organization = "-";
		String proxy = "-";
		String type = "-";
		String assignment = "-";
		String country = "-";
		String state = "-";
		String city = "-";
		
		String content = "";
		if(ip!=null&&ip.length()>0&&Pattern.matches("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$", ip))
		{
			try {
				
				URL dataURL = new URL("http://whatismyipaddress.com/ip/"+ip);
				
				BufferedReader reader = new BufferedReader(new InputStreamReader(dataURL.openStream(), Charset.forName("iso-8859-1")));
				
				char[] buf = new char[102102];
				int numRead=0;
		        while((numRead=reader.read(buf)) != -1){
		            String readData = String.valueOf(buf, 0, numRead);
		            content += readData;
		            buf = new char[102102];
		        }
		        reader.close();
				
				Document doc = Jsoup.parse(content);
	
				try {
					jsp = doc.select("div[id=section_content]").select("table").get(0).select("td").get(1).text();
					organization=doc.select("div[id=section_content]").select("table").get(0).select("td").get(2).text();
					proxy=doc.select("div[id=section_content]").select("table").get(0).select("td").get(3).text();
					type=doc.select("div[id=section_content]").select("table").get(0).select("td").get(4).text();
					assignment=doc.select("div[id=section_content]").select("table").get(0).select("td").get(5).text();
					country=doc.select("div[id=section_content]").select("table").get(1).select("td").get(0).text();
					state=doc.select("div[id=section_content]").select("table").get(1).select("td").get(1).text();
					city=doc.select("div[id=section_content]").select("table").get(1).select("td").get(2).text();
					
	
				} catch (Exception e) {
					// TODO: handle exception
				}
	
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
		req.setAttribute("jsp", jsp);
		req.setAttribute("organization", organization);
		req.setAttribute("proxy", proxy);
		req.setAttribute("type", type);
		req.setAttribute("assignment", assignment);
		req.setAttribute("country", country);
		req.setAttribute("state", state);
		req.setAttribute("city", city);
		req.setAttribute("ip", ip);
		try {
			req.getRequestDispatcher("/ip.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
